package com.meko.tms.core.access;

import com.alibaba.fastjson.JSON;
import com.meko.tms.core.service.ObtainDataService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

/***
 * @ClassName     AbstractAccessData
 * @Description   TODO 数据获取模板类
 * @Author        meko
 * @Date       2022/01/11 下午 08:38
 * @version:      1.0
 */
@Slf4j
public abstract class AbstractAccessData {

    final ObtainDataService obtainDataService;

    public AbstractAccessData(ObtainDataService manager){
        this.obtainDataService = manager;
    }

    /**
     * 数据接入
     *
     * @return
     */
    public boolean accessData(){
        log.info("开始接入数据");
        List<Object> data = getData();
        solutionData(data);
        if (incrementalAdditionData(data)) {
            log.info("接入数据成功:", JSON.toJSONString(data));
            return true;
        }
        log.info("接入数据失败:",JSON.toJSONString(data));
        return false;
    }


    /**
     * 获取数据
     *
     * @return
     */
    protected <T> List<T> getData(){
        return obtainDataService.obtainData();
    }


    /**
     * 处理数据
     *
     * @param objectList
     * @return
     */
    protected abstract <T> void solutionData(List<T> objectList);

    /**
     * 插入数据
     *
     * @param list
     * @return
     */
    protected abstract <T> boolean incrementalAdditionData(List<T> list);


}
